<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" >
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="permissions" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" label="序号" width="60">
			</el-table-column>
      <el-table-column prop="name" label="权限名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="url" label="资源路径" width="220" sortable>
      </el-table-column>
      <el-table-column prop="descs" label="权限描述" width="160" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="权限编码" width="220" sortable>
      </el-table-column>
			<el-table-column label="操作" width="160">
				<template scope="scope">
					<el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 批量删除和分页栏 -->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="totals" :current-page="currentPage" style="float:right;">
			</el-pagination>
		</el-col>

		<!-- 编辑界面 -->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">

        <el-form-item label="权限名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="资源路径" prop="url">
          <el-input v-model="saveForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限描述" prop="descs">
          <el-input v-model="saveForm.descs" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
			  // 1.分页查询的模型数据
        permissions: [],
        totals: 0,
        listLoading: false,
        currentPage: 1,
        pageSize: 5,

        // 2.高级查询的模型数据
        keyword: '',

        // 3.批量删除
        sels: [],//列表选中列

        // 4.添加和修改的模型数据
				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
        title: '',
				saveFormRules: {
					sn: [
						{ required: true, message: '请输入权限编号', trigger: 'blur' }
					],
          name: [
            { required: true, message: '请输入权限名称', trigger: 'blur' }
          ]
				},
				//编辑界面数据
				saveForm: {
					id: null,
          sn: '',
					name: '',
          url: null,
          descs: null
				},
			}
		},
		methods: {
      //1.获取权限列表
      getPermissions() {
        let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.keyword
        };
        this.listLoading = true;
        this.$http.post("/permission", para).then(res => {
          this.permissions = res.data.data;
          this.totals = res.data.totals;
          this.listLoading = false;
        }).catch(res => {
          this.$message.error("网络繁忙，请稍后重试！");
        })
      },

      // 2.点击实现分页查询方法
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getPermissions();
      },

      // 3.高级查询的方法
      keywordQuery() {
        // 将当前页更置为1，防止在其他页查询时出现bug
        this.currentPage = 1;
        this.getPermissions();
      },

      // 4.删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => { // 点击确认
          this.listLoading = true; // 打开加载框
          this.$http.delete("/permission/" + row.id).then(res => {
            if (res.data.success) {
              // 当删除掉当前页的最后一条数据后，跳转到前一页，如果当前页是最后一页，删除最后一条数据后，还是停留在当前页
              let pageTotals = Math.ceil(this.totals / this.pageSize);
              if (this.currentPage == pageTotals && (this.totals - 1) % this.pageSize == 0 && this.currentPage != 1) {
                this.currentPage = this.currentPage - 1;
              }
              this.getPermissions();
            } else {
              this.$message.error("系统繁忙，请稍后重试！");
            }
            this.listLoading = false; // 关闭加载框
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试！");
          })
        }).catch(() => { // 点击取消
        });
      },

      // 5.选中后数组改变
      selsChange: function (sels) {
        this.sels = sels;
      },

      // 6.批量删除
      batchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/permission", ids).then(res => {
            if (res.data.success) {
              this.currentPage = 1;
              this.getPermissions();
            } else {
              this.$message.error("系统繁忙，请稍后重试！");
            }
            this.listLoading = false; // 关闭加载框
          }).catch(res => {
            this.$message.error("系统繁忙，请稍后重试！");
          });
        }).catch(() => { // 点击取消
        });
      },


      // 7.点击编辑按钮弹出对话框
      handleSave: function (index, row) {
        this.title = "修改";
        this.saveForm = Object.assign({}, row);
        this.saveFormVisible = true;
      },

      // 8.点击新增按钮弹出对话框
      handleAdd: function () {
        this.title = "新增";
        this.saveForm = {
          id: null,
          sn: '',
          name: '',
          url: null,
          descs: null
        }
        this.saveFormVisible = true;
      },

      // 9.添加和修改的方法
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              this.$http.put("/permission", this.saveForm).then(res => {
                this.saveLoading = false;
                if (res.data.success) {
                  this.getPermissions();
                } else {
                  this.$message.error("系统繁忙，请稍后重试！");
                }
                this.listLoading = false;
              }).catch(res => {
                this.$message.error("系统繁忙，请稍后重试！");
              });
              this.saveFormVisible = false;
            });
          }
        });
      },
    },
		mounted() {
			this.getPermissions();
		}
	}
</script>

<style scoped>

</style>